/**
 * 
 */
package mycsveditor.editors;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;

/**
 * @author dongxu
 * 
 */
public class LineInfo {

	private IDocument document;

	private String line;

	private int offset;

	private int offsetInLine;

	private IRegion region;

	public LineInfo(IDocument document, int offset) {
		super();
		this.document = document;
		this.offset = offset;
		try {
			region = document.getLineInformationOfOffset(offset);
			line = document.get(region.getOffset(), region.getLength());
		} catch (BadLocationException ex) {
			region = new Region(0, 0);
			line = "";
		}

		offsetInLine = offset - region.getOffset();
	}

	public IDocument getDocument() {
		return document;
	}

	public String getLine() {
		return line;
	}

	public int getOffset() {
		return offset;
	}

	public int getOffsetInLine() {
		return offsetInLine;
	}

	public IRegion getRegion() {
		return region;
	}

	public String getLinePart() {
		return line.substring(0, offsetInLine);
	}
	
	public char getLastInput() {
		return line.charAt(offsetInLine - 1);
	}
}
